<?php
error_reporting(E_ALL ^ E_NOTICE);
if ($_GET == null || $_GET['code'] == null) {                                     //if-1
?>
<html>
    <head>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
        <title>Auto Docs - Editor</title>
    </head>
    <body>
        Lỗi dữ liệu.
    </body>
</html>
<?php
} else {                                                                        //else-if-1
    $codeId = $_GET['code'];
    $fileName= base64_decode(base64_decode($codeId));
    if (!file_exists($fileName) || !is_file($fileName)) {                       //if-2
?>
<html>
    <head>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
        <title>Auto Docs - Editor</title>
    </head>
    <body>
        Tập tin không chính xác.
    </body>
</html>
<?php
    } else {                                                                    //else-if-2
?>
<html>
    <head>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
        <title>Auto Docs - Editor</title>
        
        <link rel="stylesheet" href="style/main.css"/>
        <link type="text/css" href="style/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />

        <script type="text/javascript" src="libs/pdf.js"></script>
        <script type="text/javascript" src="libs/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="libs/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript" src="libs/ajaxfileupload.js"></script>
        <script type="text/javascript">
            var ratio=1.34;
            var filename = '<?php echo $fileName; ?>';
            PDFJS.workerSrc = "libs/pdf.js";
            PDFJS.getPdf(filename, function getPdfFile(data) {
                var pdf = new PDFJS.PDFDoc(data);
                var pageNum = pdf.numPages;
                var pages=[];
                var scale = ratio;
                var i;
                for (i=1;i<=pageNum;i++) {
                    pages.push(pdf.getPage(i));
                }
                var ul = document.createElement("ul");
                for (i=0;i<pageNum;i++) {
                    var div_page = document.createElement("div");
                    div_page.id = "page-" + i;

                    var anchor = document.createElement("a");
                    anchor.id = "page-a-" + i;

                    var canvas = document.createElement("canvas");
                    canvas.id = 'canvas-' + i;
                    canvas.innerHTML = "Trình duy\u1ec7t của bạn không hỗ trợ CANVAS.";
                    var context = canvas.getContext('2d');
                    pages[i].startRendering(context);
                    canvas.height = pages[i].height * scale;
                    canvas.width = pages[i].width * scale;
                    var l = "left:5px;";
                    var t = "top:" + (canvas.height * i + 10 * (i+1)) + "px;";
                    canvas.setAttribute("style", l+t + "z-index:0;");

                    var layerBeforeCanvas = document.createElement("div");
                    layerBeforeCanvas.id = "layer-for-canvas-" + i;
                    layerBeforeCanvas.className = "layer-before-canvas";
                    var h = "height:"+ canvas.height + "px;";
                    var w = "width:"+ canvas.width + "px;";
                    layerBeforeCanvas.setAttribute("style", h+w+l+t + "z-index:1;");

                    div_page.appendChild(anchor);
                    div_page.appendChild(canvas);
                    div_page.appendChild(layerBeforeCanvas);
                    if (i == (pageNum-1)) {
                        var lastElement = document.createElement("canvas");
                        lastElement.height = "1";
                        lastElement.width = canvas.width;
                        lastElementTop = "top:" + (canvas.height * (i+1) + 10 * (i+2)) + "px;";
                        lastElement.setAttribute("style", l+lastElementTop + "background-color:#e0dfe4;");
                        div_page.appendChild(lastElement);
                    }
                    document.getElementById("viewer").appendChild(div_page);
                    var li = document.createElement("li");
                    var goTo = document.createElement("a");
                    goTo.setAttribute("href", "#layer-for-canvas-" + i);
                    //goTo.className = "scroll";
                    goTo.innerHTML="Trang "+ (i+1);
                    li.appendChild(goTo);
                    ul.appendChild(li);
                }
                document.getElementById("go_to_page").appendChild(ul);
            });
        </script>
        <script type="text/javascript" src="libs/main.js"></script>
    </head>
    <body>
        <div>
            <div id="left">
                <div id="add-new" class="box">
                    Thêm mới<br/>
                    <input id="pointer" class="object-button" type="button"/>
                    <input id="addText"    class="object-button" type="button"/>
                    <input id="addImage"   class="object-button" type="button"/>
                    <input id="eraser"      class="object-button" type="button"/>
                </div>
                <div id="containElements" class="box">
                    Danh sách đối tượng: <br/>
                    <ul id="objectlist"></ul></div>
                <div id="format" class="box">
                    Định dạng<br/>
                    <select name="font" style="width: 70%;">
                        <option>Tahoma</option>
                        <option>Verdana</option>
                    </select>
                    <select name="font-size" style="width: 25%;">
                        <option>10</option>
                        <option>20</option>
                    </select>
                    <input id="format-bold" class="format-button" type="button"/>
                    <input id="format-italic" class="format-button" type="button"/>
                    <input id="format-underline" class="format-button" type="button"/>
                    <input id="format-strikethrough" class="format-button" type="button"/>
                </div>
                <div id="color" class="box">
                    Màu sắc<br/>
                    <form name="colorForm">  
                       <p>Select Background Color  
                       <input name="colorpicker" type="datetime"/>  
                       </p>  
                       <p>  
                       Selected Color  
                       <input name="selectedcolor" type="text">  
                       </p>  
                    </form>
                </div>
                <div id="functions" class="box">
                    <input id="exportPdf" type="button" value="Export pdf now!"/>
                    <input type="hidden" id="code" value="<?php echo $codeId; ?>"/>
                    <input id="exportZip" type="button" value="Export to Zip"/>
                    <input type="hidden" id="download_token_value_id"/>
                </div>
            </div>
            <div id="viewer"></div>
        </div>
        <div id="go_to_page" class="positioning"></div>
        <div id="inputDialog" style="display:none;" title="Nhập liệu">
            <p>Nhập văn bản cho ô dữ liệu</p>
            <input type="text" id="inputText"/>
        </div>
        <div id="helpDialog" style="display:none;" title="Hướng dẫn">
            <p>Hướng dẫn ban đầu. Vui lòng đọc kỹ trước khi sử dụng!</p>
            <div id="warmingVar"></div>
        </div>
        <div id="uploadDialog" style="display:none;" title="Tải tập tin">
            <img id="loading" src="loading.gif" style="display:none;">
            <form name="form" action="" method="POST" enctype="multipart/form-data">
                Chọn hình ảnh và bấm "Tải lên"<br/>
                <input id="fileToUpload" type="file" size="20" name="fileToUpload" class="input"/>
            </form>
        </div>
    </body>
</html>
<?php
    }                                                                           //end-if-2
}                                                                               //end-if-1
?>